<!-- src\components\CommonHeader.vue -->
<template>
  <div class="header-container">
    <div class="l-content">
      <el-button style="margin-right: 15px" @click="handleMenu" icon="el-icon-menu" size="mini"></el-button>
      <!-- 面包屑 -->
      <el-breadcrumb separator="/">
        <!-- 注意这里，连续点击两次会报错，原因是vue-router  实例上的 push 方法返回的是 promise 对象，所以传入的参数期望有一个成功和失败的回调，如果省略不写则会报错。
        解决方案一：每次使用 push 方法时带上两个回调函数
        解决方案二：重写 Vue-router 原型对象上的 push 函数
        解决方案三：写个方法，判断时候连续点击两下，点击两次直接就不跳转
      -->
        <el-breadcrumb-item @click.native="baread(item)" v-for="item in tags" :key="item.path">{{ item.label
          }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="r-content">
      <el-dropdown @command="handleClick">
        <span class="el-dropdown-link">
          <img class="user" src="../assets/images/user.png" alt="" />
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item>个人中心</el-dropdown-item>
          <el-dropdown-item command="cancel">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex"
import Cookie from "js-cookie";
export default {
  name: 'common-header',
  data() {
    return {}
  },
  methods: {
    handleMenu() {
      this.$store.commit("collapseMenu");
    },
    handleClick(command) {
      if (command === "cancel") {
        // 清除cookie中的token
        Cookie.remove("token")
        // 清除cookie中的menu
        Cookie.remove("menu");
        // 跳转到登录页
        this.$router.push("/login")
      }
    },
    baread(item) {
      // console.log(item)
      if (
        this.$route.path !== item.path &&
        !(this.$route.path === '/home' && item.path === '/')
      ) {
        this.$router.push(item.path)
      }
    }

  },
  computed: {
    ...mapState({
      tags: (state) => state.tab.tabsList,
    })
  }
}
</script>

<style lang="less" scoped>
.header-container {
  padding: 0 20px;
  background-color: #333;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .text {
    color: #fff;
    font-size: 14px;
    margin-left: 10px;
  }

  .r-content {
    .user {
      width: 40px;
      height: 40px;
      border-radius: 50%;
    }
  }

  .l-content {
    display: flex;
    align-items: center;

    // 样式穿透
    /deep/.el-breadcrumb__item {
      .el-breadcrumb__inner {
        font-weight: normal;

        &.is-link {
          color: #666;
        }
      }

      &:last-child {
        .el-breadcrumb__inner {
          color: #fff;
        }
      }
    }
  }

}
</style>
